//
//  FYEmoticonPackage.swift
//  dugeweibo
//
//  Created by pelesys on 16/8/10.
//  Copyright © 2016年 杜方宜. All rights reserved.
//

import UIKit

class FYEmoticonPackage: NSObject {
    var emotions : [FYEmotion] = [FYEmotion]()
    init(id : String) {
        super.init()
        //1.最近分组
        if id == "" {
            addEmptyEmotion(isRecently: true)
            return
        }
        //2.根据id拼接info.plist的路径
        let plistPath = Bundle.main.path(forResource: "\(id)/info.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        //3.根据plist文件的路径读取数据 [[String : String]]
        let array = NSArray(contentsOfFile: plistPath)! as! [[String : String]]
        //4.遍历数组
        var index = 0
        for var dict in array{
            if let png = dict["png"] {
                dict["png"] = id + "/" + png
            }
            emotions.append(FYEmotion(dict: dict))
            index += 1
            if index == 20 {
                //添加删除表情
                emotions.append(FYEmotion(isRemove: true))
                index = 0
            }
            
        }
        //5.添加空白表情
        addEmptyEmotion(isRecently: false)
        
        
    }
    
    private func addEmptyEmotion(isRecently : Bool){
        let count = emotions.count % 21
        if count == 0 && !isRecently {
            return
        }
        for _ in count..<20{
            emotions.append(FYEmotion(isEmpty: true))
            
        }
        emotions.append(FYEmotion(isRemove: true))
        
    }
}
